
const express = require('express')
const app = express()
const Mock=require('mockjs')
const {list}=Mock.mock({
	"list|10-100":[{
		"id":"@id",
		"title":"@ctitle(2)",
		"img":"@image(100x100,@color)"
	}]
})

app.get('/search',(req,res)=>{
	let {items}=req.query
	if (items!=='') {
		let itemsList=list.filter((item)=>item.title.includes(items))
		res.send({
			code:200,
			items:itemsList
		})
	}else{
		 res.send({
		 	code:200,
		 	items:list
		 })
	}
})










const user=[{
	"username":"admin",
	"password":"admin"
},{
	"username":"user",
	"password":"user"
}]
app.get('/login',function(req,res){
	let {username,password}=req.query
	console.log(username,password)
	let items=user.find((item)=>item.username==username && item.password==password)
	if(items){
		res.send({
			code:200,
			msg:"登录成功"
		})
	}else{
		res.send({
			code:400,
			msg:"登录失败"
		})
	}
})
app.get('/list', function (req, res) {
  res.send({
	  code:200,
	  list
  })
})

app.listen(3000,()=>console.log("服务启动成功"))